﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using CrystalDecisions;
using CrystalDecisions.Enterprise;
using CrystalDecisions.CrystalReports;
using CrystalDecisions.ReportSource;
using CrystalDecisions.CrystalReports.Engine;
using TI_BAL;

using CrystalDecisions.Shared;

namespace SecuritySystem.Reports.Datasets
{
    

    public partial class ReportForm : Form
    {
        class ProductGroup
        {
            public string ProductType { get; set; }
            public string SubType { get; set; }
            public string Size { get; set; }
            public decimal Quantity { get; set; }
            public decimal TotalPrice { get; set; }

        }
        class InventoryReportData
        {
            public string ProductType { get; set; }
            public string SubType { get; set; }
            public string Size { get; set; }
            public decimal Quantity { get; set; }
            public decimal UnitPrice { get; set; }
        }

        class RecievableGroup
        {
            public string Customer_Name { get; set; }
            public double Credit_Amount { get; set; }
            public double Debit_Amount { get; set; }
        }


        TIEntities dbContext;
        public string ReportType = "";
        public int InvoiceId = -1;

        public ReportForm()
        {
            InitializeComponent();
            dbContext = new TIEntities();
        }

        private void ReportForm_Load(object sender, EventArgs e)
        {
            try
            {
                if (ReportType == "SupplierReport")
                {
                    try
                    {
                        var listOfSuppliers = (from sup in dbContext.Suppliers where sup.ContactPersonName != "--" select new { sup.ContactPersonName, sup.CompanyName, sup.SupplierPhoneNumber, sup.SupplierEmailAddress, sup.City, sup.SupplierAddress }).ToList();


                        SupplierReport reportobj = new SupplierReport();

                        reportobj.SetDataSource(listOfSuppliers);
                        ParameterFieldDefinitions crParameterFieldDefinitions;
                        ParameterFieldDefinition crParameterFieldDefinition;
                        ParameterValues crParameterValues = new ParameterValues();
                        ParameterDiscreteValue crParameterDiscreteValue = new ParameterDiscreteValue();

                        crParameterDiscreteValue.Value = System.Configuration.ConfigurationSettings.AppSettings["CompanyName"];
                        crParameterFieldDefinitions = reportobj.DataDefinition.ParameterFields;
                        crParameterFieldDefinition = crParameterFieldDefinitions[0];
                        crParameterValues = crParameterFieldDefinition.CurrentValues;

                        crParameterValues.Clear();
                        crParameterValues.Add(crParameterDiscreteValue);
                        crParameterFieldDefinition.ApplyCurrentValues(crParameterValues);

                        ParameterFieldDefinitions crParameterFieldDefinitions1;
                        ParameterFieldDefinition crParameterFieldDefinition1;
                        ParameterValues crParameterValues1 = new ParameterValues();
                        ParameterDiscreteValue crParameterDiscreteValue1 = new ParameterDiscreteValue();

                        crParameterDiscreteValue1.Value = System.Configuration.ConfigurationSettings.AppSettings["CompanyAddress"];
                        crParameterFieldDefinitions1 = reportobj.DataDefinition.ParameterFields;
                        crParameterFieldDefinition1 = crParameterFieldDefinitions1[1];
                        crParameterValues1 = crParameterFieldDefinition1.CurrentValues;

                        crParameterValues1.Clear();
                        crParameterValues1.Add(crParameterDiscreteValue1);
                        crParameterFieldDefinition1.ApplyCurrentValues(crParameterValues1);

                        this.crystalReportViewer1.ReportSource = reportobj;
                    }
                    catch
                    {

                    }
                }
                else if (ReportType == "CustomerReport")
                {
                    try
                    {
                        var listOfCustomers = (from sup in dbContext.Customers where sup.CustomerName != "--" select new { sup.CustomerName, sup.CustomerCompanyName, sup.CustomerPhoneNumber, sup.CustomerEmailAddress, sup.City, sup.CustomerAddress }).ToList();


                        CustomerReport reportobj = new CustomerReport();

                        reportobj.SetDataSource(listOfCustomers);

                        ParameterFieldDefinitions crParameterFieldDefinitions;
                        ParameterFieldDefinition crParameterFieldDefinition;
                        ParameterValues crParameterValues = new ParameterValues();
                        ParameterDiscreteValue crParameterDiscreteValue = new ParameterDiscreteValue();

                        crParameterDiscreteValue.Value = System.Configuration.ConfigurationSettings.AppSettings["CompanyName"];
                        crParameterFieldDefinitions = reportobj.DataDefinition.ParameterFields;
                        crParameterFieldDefinition = crParameterFieldDefinitions[0];
                        crParameterValues = crParameterFieldDefinition.CurrentValues;

                        crParameterValues.Clear();
                        crParameterValues.Add(crParameterDiscreteValue);
                        crParameterFieldDefinition.ApplyCurrentValues(crParameterValues);

                        ParameterFieldDefinitions crParameterFieldDefinitions1;
                        ParameterFieldDefinition crParameterFieldDefinition1;
                        ParameterValues crParameterValues1 = new ParameterValues();
                        ParameterDiscreteValue crParameterDiscreteValue1 = new ParameterDiscreteValue();

                        crParameterDiscreteValue1.Value = System.Configuration.ConfigurationSettings.AppSettings["CompanyAddress"];
                        crParameterFieldDefinitions1 = reportobj.DataDefinition.ParameterFields;
                        crParameterFieldDefinition1 = crParameterFieldDefinitions1[1];
                        crParameterValues1 = crParameterFieldDefinition1.CurrentValues;

                        crParameterValues1.Clear();
                        crParameterValues1.Add(crParameterDiscreteValue1);
                        crParameterFieldDefinition1.ApplyCurrentValues(crParameterValues1);


                        this.crystalReportViewer1.ReportSource = reportobj;
                    }
                    catch
                    {

                    }
                }
                else if (ReportType == "InvoiceReport" && InvoiceId != -1)
                {
                    try
                    {
                        var InvoiceReport = (from j in dbContext.SalesTransactions.Include("ProductType").Include("ProductSubType").Include("ProductSize")
                                             where j.INVOICE_ID == InvoiceId
                                             select new
                                             {
                                                 TypeName = j.ProductType.Name,
                                                 SubTypeName = j.ProductSubType.Name,
                                                 SizeName = j.ProductSize.Name,
                                                 PER_UNIT_PRICE = j.PER_UNIT_PRICE.Value,
                                                 QUANTITY = j.QUANITY.Value,
                                                 INVOICE_ID = j.INVOICE_ID.Value,
                                                 CustomerCompanyName= j.Customer.CustomerCompanyName
                                             }).ToList();

                        
                        //SupplierReport reportobj = new SupplierReport();
                        InvoiceReport reportobj = new InvoiceReport();

                        reportobj.SetDataSource(InvoiceReport);
                        ParameterFieldDefinitions crParameterFieldDefinitions;
                        ParameterFieldDefinition crParameterFieldDefinition;
                        ParameterValues crParameterValues = new ParameterValues();
                        ParameterDiscreteValue crParameterDiscreteValue = new ParameterDiscreteValue();

                        crParameterDiscreteValue.Value = System.Configuration.ConfigurationSettings.AppSettings["CompanyName"];
                        crParameterFieldDefinitions = reportobj.DataDefinition.ParameterFields;
                        crParameterFieldDefinition = crParameterFieldDefinitions[0];
                        crParameterValues = crParameterFieldDefinition.CurrentValues;

                        crParameterValues.Clear();
                        crParameterValues.Add(crParameterDiscreteValue);
                        crParameterFieldDefinition.ApplyCurrentValues(crParameterValues);

                        ParameterFieldDefinitions crParameterFieldDefinitions1;
                        ParameterFieldDefinition crParameterFieldDefinition1;
                        ParameterValues crParameterValues1 = new ParameterValues();
                        ParameterDiscreteValue crParameterDiscreteValue1 = new ParameterDiscreteValue();

                        crParameterDiscreteValue1.Value = System.Configuration.ConfigurationSettings.AppSettings["CompanyAddress"];
                        crParameterFieldDefinitions1 = reportobj.DataDefinition.ParameterFields;
                        crParameterFieldDefinition1 = crParameterFieldDefinitions1[1];
                        crParameterValues1 = crParameterFieldDefinition1.CurrentValues;

                        crParameterValues1.Clear();
                        crParameterValues1.Add(crParameterDiscreteValue1);
                        crParameterFieldDefinition1.ApplyCurrentValues(crParameterValues1);
                        this.crystalReportViewer1.ReportSource = reportobj;
                    }

                    catch
                    {

                    }
                }
                else if (ReportType == "LedgerReport")
                {
                    List<JournalGroup> jGroupList = (from j in dbContext.Journals
                                                     group j by new { AccountName = j.Account.Account_Name, TransactionType = j.TransactionType }
                                                         into grp
                                                         select new JournalGroup()
                                                         {
                                                             AccountName = grp.Key.AccountName,
                                                             TransactionType = grp.Key.TransactionType,
                                                             Amount = grp.Sum(t => t.Journal_Amount.Value)

                                                         }).ToList();

                    LedgerReport reportobj = new LedgerReport();

                    reportobj.SetDataSource(jGroupList);
                    ParameterFieldDefinitions crParameterFieldDefinitions;
                    ParameterFieldDefinition crParameterFieldDefinition;
                    ParameterValues crParameterValues = new ParameterValues();
                    ParameterDiscreteValue crParameterDiscreteValue = new ParameterDiscreteValue();

                    crParameterDiscreteValue.Value = System.Configuration.ConfigurationSettings.AppSettings["CompanyName"];
                    crParameterFieldDefinitions = reportobj.DataDefinition.ParameterFields;
                    crParameterFieldDefinition = crParameterFieldDefinitions[0];
                    crParameterValues = crParameterFieldDefinition.CurrentValues;

                    crParameterValues.Clear();
                    crParameterValues.Add(crParameterDiscreteValue);
                    crParameterFieldDefinition.ApplyCurrentValues(crParameterValues);

                    ParameterFieldDefinitions crParameterFieldDefinitions1;
                    ParameterFieldDefinition crParameterFieldDefinition1;
                    ParameterValues crParameterValues1 = new ParameterValues();
                    ParameterDiscreteValue crParameterDiscreteValue1 = new ParameterDiscreteValue();

                    crParameterDiscreteValue1.Value = System.Configuration.ConfigurationSettings.AppSettings["CompanyAddress"];
                    crParameterFieldDefinitions1 = reportobj.DataDefinition.ParameterFields;
                    crParameterFieldDefinition1 = crParameterFieldDefinitions1[1];
                    crParameterValues1 = crParameterFieldDefinition1.CurrentValues;

                    crParameterValues1.Clear();
                    crParameterValues1.Add(crParameterDiscreteValue1);
                    crParameterFieldDefinition1.ApplyCurrentValues(crParameterValues1);
                    this.crystalReportViewer1.ReportSource = reportobj;

                }
                else if (ReportType == "AllPartyRecievable")
                {
                    var listOfRecievables = (from j in dbContext.Journals
                                             where j.Account_ID == 4
                                             group j by new { j.Customer.CustomerCompanyName } into jg
                                             select new RecievableGroup
                                             {
                                                 Customer_Name = jg.Key.CustomerCompanyName,
                                                 Debit_Amount = jg.Sum(j => j.Debit_Amount).Value,
                                                 Credit_Amount = jg.Sum(j => j.Credit_Amount).Value,

                                             }).ToList();



                    CrystalReportAR reportobj = new CrystalReportAR();

                    reportobj.SetDataSource(listOfRecievables);

                    ParameterFieldDefinitions crParameterFieldDefinitions;
                    ParameterFieldDefinition crParameterFieldDefinition;
                    ParameterValues crParameterValues = new ParameterValues();
                    ParameterDiscreteValue crParameterDiscreteValue = new ParameterDiscreteValue();

                    crParameterDiscreteValue.Value = System.Configuration.ConfigurationSettings.AppSettings["CompanyName"];
                    crParameterFieldDefinitions = reportobj.DataDefinition.ParameterFields;
                    crParameterFieldDefinition = crParameterFieldDefinitions[0];
                    crParameterValues = crParameterFieldDefinition.CurrentValues;

                    crParameterValues.Clear();
                    crParameterValues.Add(crParameterDiscreteValue);
                    crParameterFieldDefinition.ApplyCurrentValues(crParameterValues);

                    ParameterFieldDefinitions crParameterFieldDefinitions1;
                    ParameterFieldDefinition crParameterFieldDefinition1;
                    ParameterValues crParameterValues1 = new ParameterValues();
                    ParameterDiscreteValue crParameterDiscreteValue1 = new ParameterDiscreteValue();

                    crParameterDiscreteValue1.Value = System.Configuration.ConfigurationSettings.AppSettings["CompanyAddress"];
                    crParameterFieldDefinitions1 = reportobj.DataDefinition.ParameterFields;
                    crParameterFieldDefinition1 = crParameterFieldDefinitions1[1];
                    crParameterValues1 = crParameterFieldDefinition1.CurrentValues;

                    crParameterValues1.Clear();
                    crParameterValues1.Add(crParameterDiscreteValue1);
                    crParameterFieldDefinition1.ApplyCurrentValues(crParameterValues1);


                    this.crystalReportViewer1.ReportSource = reportobj;
                    
                }
                else if (ReportType == "AllPartyPayable")
                {

                    var listOfPayables = (from j in dbContext.Journals
                                             where j.Account_ID == 5
                                             group j by new { j.Supplier.CompanyName } into jg
                                             select new RecievableGroup
                                             {
                                                 Customer_Name = jg.Key.CompanyName,
                                                 Debit_Amount = jg.Sum(j => j.Debit_Amount).Value,
                                                 Credit_Amount = jg.Sum(j => j.Credit_Amount).Value,

                                             }).ToList();



                    CrystalReportAP reportobj = new CrystalReportAP();

                    reportobj.SetDataSource(listOfPayables);

                    ParameterFieldDefinitions crParameterFieldDefinitions;
                    ParameterFieldDefinition crParameterFieldDefinition;
                    ParameterValues crParameterValues = new ParameterValues();
                    ParameterDiscreteValue crParameterDiscreteValue = new ParameterDiscreteValue();

                    crParameterDiscreteValue.Value = System.Configuration.ConfigurationSettings.AppSettings["CompanyName"];
                    crParameterFieldDefinitions = reportobj.DataDefinition.ParameterFields;
                    crParameterFieldDefinition = crParameterFieldDefinitions[0];
                    crParameterValues = crParameterFieldDefinition.CurrentValues;

                    crParameterValues.Clear();
                    crParameterValues.Add(crParameterDiscreteValue);
                    crParameterFieldDefinition.ApplyCurrentValues(crParameterValues);

                    ParameterFieldDefinitions crParameterFieldDefinitions1;
                    ParameterFieldDefinition crParameterFieldDefinition1;
                    ParameterValues crParameterValues1 = new ParameterValues();
                    ParameterDiscreteValue crParameterDiscreteValue1 = new ParameterDiscreteValue();

                    crParameterDiscreteValue1.Value = System.Configuration.ConfigurationSettings.AppSettings["CompanyAddress"];
                    crParameterFieldDefinitions1 = reportobj.DataDefinition.ParameterFields;
                    crParameterFieldDefinition1 = crParameterFieldDefinitions1[1];
                    crParameterValues1 = crParameterFieldDefinition1.CurrentValues;

                    crParameterValues1.Clear();
                    crParameterValues1.Add(crParameterDiscreteValue1);
                    crParameterFieldDefinition1.ApplyCurrentValues(crParameterValues1);


                    this.crystalReportViewer1.ReportSource = reportobj;                    


                }






            }
            catch { MessageBox.Show("There was some problem in report generation, please contact administrator"); }


            }

        private void crystalReportViewer1_Load(object sender, EventArgs e)
        {

        }
        
    }

    public class JournalGroup
    {
        public string AccountName { get; set; }
        public string TransactionType { get; set; }
        public double Amount { get; set; }

    }
}
